package com.example.actordemo3.sharding;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.cluster.sharding.ClusterSharding;
import akka.cluster.sharding.ClusterShardingSettings;
import akka.cluster.sharding.ShardRegion;
import scala.Option;

import java.util.Optional;

public class LifeParentActor extends AbstractActor {

    static ShardRegion.MessageExtractor messageExtractor = new ShardRegion.MessageExtractor() {
        @Override
        public String shardId(Object message) {
            int numberOfShards = 100;
            if (message instanceof ShardingTest.Counter.EntityEnvelope) {
                long id = ((ShardingTest.Counter.EntityEnvelope) message).id;
                return String.valueOf(id % numberOfShards);
            } else if (message instanceof ShardingTest.Counter.Get) {
                long id = ((ShardingTest.Counter.Get) message).counterId;
                return String.valueOf(id % numberOfShards);
            } else {
                return null;
            }
        }

        @Override
        public String entityId(Object message) {
            if (message instanceof ShardingTest.Counter.EntityEnvelope)
                return String.valueOf(((ShardingTest.Counter.EntityEnvelope) message).id);
            else if (message instanceof ShardingTest.Counter.Get)
                return String.valueOf(((ShardingTest.Counter.Get) message).counterId);
            else return null;
        }

        @Override
        public Object entityMessage(Object message) {
            if (message instanceof ShardingTest.Counter.EntityEnvelope)
                return ((ShardingTest.Counter.EntityEnvelope) message).payload;
            else return message;
        }
    };


    @Override
    public void preStart() throws Exception {

//        ActorRef actorRef = getContext().actorOf(Props.create(LifeChildActor.class), "lifeChildActor");

        System.out.println("parentlife  preStart ");
    }

//    @Override
//    public void aroundPreStart() {
//        System.out.println("parentlife  aroundPreStart ");
//    }

    @Override
    public void postStop() throws Exception {
        System.out.println("parentlife  postStop ");
    }

//    @Override
//    public void aroundPostStop() {
//        System.out.println("parentlife  aroundPostStop ");
//    }

//    @Override
//    public void aroundPreRestart(Throwable reason, Option<Object> message) {
//        System.out.println("parentlife  aroundPreRestart ");
//    }

    @Override
    public void preRestart(Throwable reason, Option<Object> message) throws Exception {
        System.out.println("parentlife  preRestart ");
    }

//    @Override
//    public void aroundPostRestart(Throwable reason) {
//        System.out.println("parentlife  aroundPostRestart ");
//    }

    @Override
    public void preRestart(Throwable reason, Optional<Object> message) throws Exception {
        System.out.println("parentlife  preRestart ");
    }

    @Override
    public void postRestart(Throwable reason) throws Exception {
        System.out.println("parentlife  postRestart ");
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(o->{
            System.out.println(o);
        }).build();
    }

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys1");
//        ActorRef actorRef = actorSystem.actorOf(Props.create(LifeParentActor.class), "lifeParentActor");
        ClusterShardingSettings settings = ClusterShardingSettings.create(system);
        ActorRef startedCounterRegion =
                ClusterSharding.get(system)
                        .start("lifeParentActor", Props.create(LifeParentActor.class), settings,messageExtractor );
        ActorRef counterRegion = ClusterSharding.get(system).shardRegion("lifeParentActor");
        counterRegion.tell("1111",ActorRef.noSender());
//
    }
}
